/**	
 * <br>
 * Copyright 2012 Ec.Sgcc. All rights reserved.<br>
 * <br>			 
 * Package: com.chuan.util <br>
 * FileName: ImageUtil.java <br>
 * <br>
 * @version
 * @author chen_yc@ec.sgcc.com.cn
 * @created 2013/02/28
 * @last Modified 
 * @history
 */
package com.chuan.mantu.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * {图片处理工具}
 * 
 * @author chen_yc@ec.sgcc.com.cn
 * @created 2013/02/28 10:43:07
 * @lastModified
 * @history
 */

public class ImageUtil {

	/**
	 * 判断文件是否为图片<br>
	 * <br>
	 * @param pInput 文件名<br>
	 * @param pImgeFlag 判断具体文件类型<br>
	 * @return 检查后的结果<br>
	 */
	public static boolean isImageSuffix(String pInput){
		// 文件名称为空的场合
		if (pInput == null || pInput.trim().equals("")) {
			// 返回不和合法
			return false;
		}
		// 获得文件后缀名
		String tmpName = pInput.substring(pInput.lastIndexOf(".") + 1, pInput.length());
		// 声明图片后缀名数组
		String imgeArray[][] = { { "bmp", "0" }, { "dib", "1" },
				{ "gif", "2" }, { "jfif", "3" }, { "jpe", "4" },
				{ "jpeg", "5" }, { "jpg", "6" }, { "png", "7" },
				{ "tif", "8" }, { "tiff", "9" }, { "ico", "10" } };
		// 遍历名称数组
		for (int i = 0; i < imgeArray.length; i++) {
			// 判断符合全部类型的场合
			if (imgeArray[i][0].equals(tmpName.toLowerCase())) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 通过读取文件并获取其width及height的方式，来判断判断当前文件是否图片，这是一种非常简单的方式。
	 * 
	 * @param imageFile
	 * @return
	 */
	public static boolean isImageWH(String filePath) {
		Bitmap bitmap = BitmapFactory.decodeFile(filePath);
		try {
			if (bitmap == null || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
				return false;
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static int computeSampleSize(BitmapFactory.Options options, int  minSideLength,  int  maxNumOfPixels) {
            int  initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);     
            int  roundedSize;
            if  (initialSize <=  8 ) {
               roundedSize =  1 ;
               while  (roundedSize < initialSize) {
                   roundedSize <<=  1 ;
               }
           }  else  {
               roundedSize = (initialSize +  7 ) /  8  *  8 ;
           }
           return  roundedSize;

      }

      private  static  int  computeInitialSampleSize(BitmapFactory.Options options,
               int  minSideLength,  int  maxNumOfPixels) {
           double  w = options.outWidth;
           double  h = options.outHeight;
           int  lowerBound = (maxNumOfPixels == - 1 ) ?  1  :
                   ( int ) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
           int  upperBound = (minSideLength == - 1 ) ?  128  :
                   ( int ) Math.min(Math.floor(w / minSideLength),
                   Math.floor(h / minSideLength));
           if  (upperBound < lowerBound) {
               // return the larger one when there is no overlapping zone.
               return  lowerBound;
           }
           if  ((maxNumOfPixels == - 1 ) &&
                   (minSideLength == - 1 )) {
               return  1 ;
           }  else  if  (minSideLength == - 1 ) {
               return  lowerBound;
           }  else  {
               return  upperBound;
           }
      } 
	
}